package cloud.health.common.file.application;

import cloud.health.common.file.application.handler.HttpChannelInboundHandler;
import cloud.health.common.file.infrastructure.AbstractNettyServer;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.stream.ChunkedWriteHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 文件服务器
 */
@Slf4j
@Component
public class FileNettyApplication extends AbstractNettyServer {

    @Value("${netty.port:7000}")
    private int port;

    @Override
    protected String name() {
        return "fileServer";
    }

    @Override
    protected int port() {
        return port;
    }

    @Override
    protected ChannelInitializer<SocketChannel> channelInitializer() {
        return new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) {
                ch.pipeline().addLast(new ChunkedWriteHandler());
                ch.pipeline().addLast(new HttpChannelInboundHandler());
            }
        };
    }

}
